package com.czkt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.czkt.entity.BasDict;
import com.czkt.entity.CstCustomer;
import com.czkt.entity.CstService;
import com.czkt.entity.SysUser;
import com.czkt.entity.vo.CstServiceAndBasDict;
import com.czkt.service.IBasDictService;
import com.czkt.service.ICstCustomerService;
import com.czkt.service.ICstServiceService;
import com.czkt.service.ISysUserService;
import com.czkt.service.impl.BasDictServiceImpl;
import com.czkt.service.impl.CstCustomerServiceImpl;
import com.czkt.service.impl.CstServiceServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.security.auth.Subject;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2024-06-18
 */
@Controller
public class CstServiceController {

    @Resource
    ISysUserService sysUserService;

    @Resource
    ICstServiceService cstServiceService;

    @Resource
    ICstCustomerService cstCustomerService;

    @Resource
    IBasDictService basDictService;

    /**
     * 服务分配列表
     * @param svrCustName 客户姓名
     * @param svrType 服务类型
     * @param svrTitle 概要
     * @param pageIndex 当前页数
     * @param pageSize 每页记录数
     * @param model 传值
     * @return 跳转到服务列表页面
     */
    @RequestMapping("/service/list")
    public String serviceList(@RequestParam(value = "svrCustName",defaultValue = "") String svrCustName,
                              @RequestParam(value = "svrType",defaultValue = "") String svrType,
                              @RequestParam(value = "svrTitle",defaultValue = "") String svrTitle,
                              @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                              @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize, Model model){
        IPage<CstService> serviceIPage = cstServiceService.selectServiceAndBasDict(svrCustName, svrType, svrTitle, pageIndex, pageSize);
        model.addAttribute("servicePage",serviceIPage);
        model.addAttribute("svrCustName",svrCustName);
        model.addAttribute("dicts",basDictService.selectDictFW());
        model.addAttribute("users",sysUserService.list());
        model.addAttribute("svrType",svrType);
        model.addAttribute("svrTitle",svrTitle);
        return "/service/list";
    }

    /**
     * 添加
     * @param model 传值
     * @return 跳转到添加页面
     */
    @RequestMapping("/service/add")
    public String insertService(Model model){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<CstCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CstCustomer::getCustManagerId,sysUser.getUsrId());
        List<CstCustomer> customers = cstCustomerService.list(queryWrapper);
        List<BasDict> basDict = basDictService.selectDictFW();
        List<BasDict> status = basDictService.selectDictStatus();
        model.addAttribute("customers",customers);
        model.addAttribute("dicts",basDict);
        model.addAttribute("status",status);
        return "/service/add";
    }

    /**
     * 添加
     * @param cstService 服务实体类
     * @return 添加成功跳转到服务分配列表
     */
    @RequestMapping("/service/save")
    public String toInsertService(CstService cstService){
        if(cstServiceService.save(cstService)){
            return "redirect:/service/list";
        }
        return "redirect:/service/add";
    }

    /**
     * 服务分配
     * @param svrCustName 客户姓名
     * @param svrType 服务类型
     * @param svrTitle 概要
     * @param pageIndex 当前页数
     * @param pageSize 每页记录数
     * @param model 传值
     * @return 跳转到服务分配页面
     */
    @RequestMapping("/service/dispatch")
    public String selectDispatch(@RequestParam(value = "svrCustName",defaultValue = "") String svrCustName,
                                 @RequestParam(value = "svrType",defaultValue = "") String svrType,
                                 @RequestParam(value = "svrTitle",defaultValue = "") String svrTitle,
                                 @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                                 @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize, Model model){
        IPage<CstService> serviceIPage = cstServiceService.selectServiceAndBasDict(svrCustName, svrType, svrTitle, pageIndex, pageSize);
        model.addAttribute("servicePage",serviceIPage);
        model.addAttribute("svrCustName",svrCustName);
        model.addAttribute("dicts",basDictService.selectDictFW());
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.lambda().eq(SysUser::getUsrRoleId,4);
        model.addAttribute("users",sysUserService.list(sysUserQueryWrapper));
        model.addAttribute("svrType",svrType);
        model.addAttribute("svrTitle",svrTitle);
        return "/service/dispatch";
    }

    /**
     * 删除
     * @param svrId 服务编号
     * @return 是否成功
     */
    @RequestMapping("/crm/service/del/{svrId}")
    @ResponseBody
    public Object serviceDel(@PathVariable("svrId") String svrId){
        return cstServiceService.removeById(svrId);
    }

    /**
     * 分配
     * @param svrId 服务编号
     * @param svrDueId 分配人编号
     * @return 是否成功
     */
    @RequestMapping("/crm/service/allot/{svrId}/{svrDueId}")
    @ResponseBody
    public Object serviceAllot(@PathVariable("svrId") Long svrId,@PathVariable("svrDueId") Long svrDueId){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getUsrId,svrDueId);
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        CstService service = new CstService();
        service.setSvrId(svrId);
        service.setSvrDealId(svrDueId);
        service.setSvrDealBy(sysUser.getUsrName());
        service.setSvrDueId(user.getUsrId());
        service.setSvrDueTo(user.getUsrName());
        service.setSvrStatus("2");
        return cstServiceService.updateById(service);
    }

    /**
     * 服务处理
     * @param svrCustName 客户姓名
     * @param svrType 服务类型
     * @param svrTitle 概要
     * @param pageIndex 当前页数
     * @param pageSize 每页记录数
     * @param model 传值
     * @return 跳转到服务处理页面
     */
    @RequestMapping("/service/deal")
    public String serviceDeal(@RequestParam(value = "svrCustName",defaultValue = "") String svrCustName,
                              @RequestParam(value = "svrType",defaultValue = "") String svrType,
                              @RequestParam(value = "svrTitle",defaultValue = "") String svrTitle,
                              @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                              @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize, Model model){
        IPage<CstService> serviceIPage = cstServiceService.selectServiceDeal(svrCustName, svrType, svrTitle, pageIndex, pageSize);
        model.addAttribute("page",serviceIPage);
        model.addAttribute("svrCustName",svrCustName);
        model.addAttribute("dicts",basDictService.selectDictFW());
        model.addAttribute("svrType",svrType);
        model.addAttribute("svrTitle",svrTitle);
        return "/service/deal";
    }

    /**
     *
     * @param svrId
     * @param model
     * @return
     */
    @RequestMapping("/service/dealEdit")
    public String serviceDealEdit(String svrId,Model model){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        CstService cstService = cstServiceService.getById(svrId);
        model.addAttribute("serviced",cstService);
        return "/service/dealEdit";

    }

    @RequestMapping("/service/editDeal")
    public String serviceEditDeal(CstService cstService){
        cstService.setSvrStatus("3");
        if(cstServiceService.updateById(cstService)){
            return "redirect:/service/deal";
        }else {
           return "forward:/service/dealEdit？svrId="+cstService.getSvrId();
        }
    }

    @RequestMapping("/service/feedback")
    public String serviceFeedback(@RequestParam(value = "svrCustName",defaultValue = "") String svrCustName,
                                  @RequestParam(value = "svrType",defaultValue = "") String svrType,
                                  @RequestParam(value = "svrTitle",defaultValue = "") String svrTitle,
                                  @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                                  @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize, Model model){
        IPage<CstService> serviceIPage = cstServiceService.selectServiceAndFeedback(svrCustName, svrType, svrTitle, pageIndex, pageSize);
        model.addAttribute("feedpage",serviceIPage);
        model.addAttribute("svrCustName",svrCustName);
        model.addAttribute("dicts",basDictService.selectDictFW());
        model.addAttribute("svrType",svrType);
        model.addAttribute("svrTitle",svrTitle);
        return "/service/feedback";
    }

    @RequestMapping("/service/toFeedBack")
    public String serviceToFeedback(String svrId,Model model){
        model.addAttribute("serviced",cstServiceService.getById(svrId));
        return "/service/feedbackEdit";
    }

    @RequestMapping("/service/editFB")
    public String serviceEditFB(CstService cstService){
        if(cstService.getSvrSatisfy() < 3){
            cstService.setSvrStatus("2");
        }else if (cstService.getSvrSatisfy() >= 3){
            cstService.setSvrStatus("4");
        }
        cstServiceService.updateById(cstService);
        return "redirect:/service/feedback";
    }

    @RequestMapping("/service/arch")
    public String serviceArch(@RequestParam(value = "svrCustName",defaultValue = "") String svrCustName,
                                  @RequestParam(value = "svrType",defaultValue = "") String svrType,
                                  @RequestParam(value = "svrTitle",defaultValue = "") String svrTitle,
                                  @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                                  @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize, Model model){
        IPage<CstServiceAndBasDict> serviceIPage = cstServiceService.selectServiceAndArch(svrCustName, svrType, svrTitle, pageIndex, pageSize);
        model.addAttribute("servicePage",serviceIPage);
        model.addAttribute("svrCustName",svrCustName);
        model.addAttribute("dicts",basDictService.selectDictFW());
        model.addAttribute("svrType",svrType);
        model.addAttribute("svrTitle",svrTitle);
        return "/service/arch";
    }

    @RequestMapping("/service/findArch")
    public String serviceFindArch(String svrId,Model model){
        model.addAttribute("serviced",cstServiceService.getById(svrId));
        return "/service/findArch";
    }
}
